package www.dianbaozu.com.ui.my

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.LoadingHelperType
import www.wen.com.common.lib.data.entities.LoadingHelperUiState
import www.wen.com.common.lib.data.entities.ProtocolEntity
import www.wen.com.common.lib.data.entities.Result
import www.wen.com.common.lib.data.repository.AppDataManager

class ContractViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
        BaseApiViewModel(appDataManager) {

    fun requestProtocol(type: String,action:(ProtocolEntity)->Unit) {
        viewModelScope.launch {
            loadingUiHelper.value = LoadingHelperUiState(LoadingHelperType.LOADING)
            val result = appDataManager.requestProtocol(mapOf("type" to type))
            if(result is Result.Success){
                loadingUiHelper.value = LoadingHelperUiState(LoadingHelperType.CONTENT)
                action.invoke(result.data)
            }else if(result is Result.Error){
                loadingUiHelper.value = LoadingHelperUiState(LoadingHelperType.ERROR, result.exception.message)
            }
        }
    }

}